package com.digitalart.site.menu {
	import flash.events.Event;

	import com.digitalart.site.commons.CustomEvent;
	import com.digitalart.site.commons.CustomXmlLoader;

	import flash.events.EventDispatcher;	

	/**
	 * @author johan.chaves
	 */
	public class MenuModel extends EventDispatcher {
		
		public static const PATH : String = "xml/menu.xml";
		public static const ON_LOAD_DATA_READY : String = "onloaddataready";
		
		private var _horizontalMenuItems : Array;
		private var _verticalMenuItems : Array;				
				
		private var _data : XML;
		
		public function MenuModel() {					
			super(this);			
		}
		
		public function init() : void{
			
			horizontalMenuItems = new Array();
			verticalMenuItems = new Array();
			var loader : CustomXmlLoader = CustomXmlLoader.getInstance();
			loader.addEventListener(CustomXmlLoader.XML_LOAD_COMPLETE, dataLoaded);
			loader.loadXml(PATH);
		}
		
		private function dataLoaded(e:CustomEvent) : void {
			
			e.target.removeEventListener(CustomXmlLoader.XML_LOAD_COMPLETE, dataLoaded);
			_data = e.data;
			createMenuItems();
		}	

		private function createMenuItems() : void {
			
			createHorizontalItems();
			createVerticalItems();
			dispatchEvent(new Event(ON_LOAD_DATA_READY));
		}

		private function createVerticalItems() : void {
			
			var itemVO : MenuItemVO;
			
			for each(var itemData : XML in _data.horizontal.item){
				itemVO = new MenuItemVO();
				itemVO.id = itemData.@sectionId;
				itemVO.text = itemData;	
				itemVO.type = itemData.@type;			
				horizontalMenuItems.push(itemVO);
			}
		}

		private function createHorizontalItems() : void {
			
			var itemVO : MenuItemVO;
			
			for each(var itemData : XML in _data.vertical.item){				
				itemVO = new MenuItemVO();
				itemVO.id = itemData.@sectionId;
				itemVO.text = itemData;
				itemVO.type = itemData.@type;
				verticalMenuItems.push(itemVO);
			}
		}

		public function get horizontalMenuItems() : Array {
			return _horizontalMenuItems;
		}
		
		public function set horizontalMenuItems(menuItems : Array) : void {
			_horizontalMenuItems = menuItems;
		}
		
		public function get verticalMenuItems() : Array {
			return _verticalMenuItems;
		}
		
		public function set verticalMenuItems(verticalMenuItems : Array) : void {
			_verticalMenuItems = verticalMenuItems;
		}
	}
}
